package com.yang.frost.blueberry.service.impl;

import com.yang.frost.blueberry.constant.CommonConstant;
import com.yang.frost.blueberry.domain.IpAddress;
import com.yang.frost.blueberry.mapper.IpAddressMapper;
import com.yang.frost.blueberry.service.IpAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 10296
 * @date 2021/1/19
 * @Description:
 */
@Service
public class IpAddressServiceImpl implements IpAddressService {

    @Autowired
    private IpAddressMapper ipAddressMapper;


    @Override
    public int insertIpAddress(Map<String, Object> paramsMap) {
        int i = 0;
        try {
            i = ipAddressMapper.insertIpAddress(paramsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return i;
    }

    @Override
    public IpAddress getAddressByIp(String ip) {

        if (StringUtils.isBlank(ip)) {
            return null;
        }
        if (CommonConstant.LOCALHOST_IP.equals(ip)) {
            IpAddress ipAddress = new IpAddress();
            ipAddress.setArea(CommonConstant.LOCALHOST_IP_NAME);
            return ipAddress;
        }
        if (CommonConstant.LAN_IP.equals(ip)) {
            IpAddress ipAddress = new IpAddress();
            ipAddress.setArea(CommonConstant.LAN_IP_NAME);
            return ipAddress;
        }

        return ipAddressMapper.getAddressByIp(ip);

    }

    @Override
    public String getAreaByIp(String ip) {
        IpAddress ipAddress = getAddressByIp(ip);
        if (ipAddress != null) {
            return ipAddress.getArea();
        }
        return null;
    }

    @Override
    public String getOperatorByIp(String ip) {
        IpAddress ipAddress = getAddressByIp(ip);
        if (ipAddress != null) {
            return ipAddress.getOperator();
        }
        return null;
    }
}
